function Preference() {
}

Preference.prototype = {
   __prefs: null,
   prefs: function() {
      if (this.__prefs==null)
         this.__prefs = Components.classes["@mozilla.org/preferences-service;1"].
                  getService(Components.interfaces.nsIPrefBranch);

      return this.__prefs;
   },
   get: function(key, def, type) {
      switch(type) {
         case 'char':
               try {
                  return this.prefs().getPrefType(key) == this.prefs().PREF_STRING? this.prefs().getCharPref(key): def;
               } catch(e) {
                  return def;
               }
         case 'int':
            try {
                  return this.prefs().getPrefType(key) == this.prefs().PREF_INT? this.prefs().getIntPref(key): def;
               } catch(e) {
                  return def;
               }
         case 'bool':
            try {
                  return this.prefs().getPrefType(key) == this.prefs().PREF_BOOL? this.prefs().getBoolPref(key): def;
               } catch(e) {
                  return def;
               }
      }

      return def;
   },
   getBool: function(key, def) {
      return this.get(key, def, 'bool');
   },
   getInt: function(key, def) {
      return this.get(key, def, 'int');
   },
   getString: function(key, def) {
      return this.get(key, def, 'char');
   },
   setBool: function(key, val) {
      this.prefs().setBoolPref(key, val);
   },
   setInt: function(key, val) {
      this.prefs().setIntPref(key, val);
   },
   setString: function(key, val) {
      this.prefs().setCharPref(key, val);
   }
}

if (typeof YAHOO.MoodMill.Preference == "undefined")
    YAHOO.MoodMill.Preference = new Preference();